﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

namespace AK.FTP
{
    public partial class FrmFileSync : Form
    {
        public FrmFileSync()
        {
            InitializeComponent();
        }

        private void FrmFileSync_Load(object sender, EventArgs e)
        {

        }

        private void btnOpenSource_Click(object sender, EventArgs e)
        {
            if (fbdFile.ShowDialog() == DialogResult.OK)
            {
                txtSource.Text = fbdFile.SelectedPath;
            }
        }

        private void btnOpenTarget_Click(object sender, EventArgs e)
        {
            if (fbdFile.ShowDialog() == DialogResult.OK)
            {
                txtTarget.Text = fbdFile.SelectedPath;
            }
        }

        private void btnStartSync_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(txtSource.Text))
            {
                MessageBox.Show("源目录不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtTarget.Text))
            {
                MessageBox.Show("目标目录不能为空");
                return;
            }

            if (!Directory.Exists(txtSource.Text))
            {
                MessageBox.Show("源目录不存在");
                return;
            }

            if (!Directory.Exists(txtTarget.Text))
            {
                MessageBox.Show("源目录不存在");
                return;
            }

            if (string.IsNullOrEmpty(txtFiles.Text))
            {
                MessageBox.Show("无文件需要复制");
                txtFiles.Focus();
                return;
            }
            #endregion

            string targetPath = txtTarget.Text;
            string sourcePath = txtSource.Text;
            Stopwatch sw = Stopwatch.StartNew();
            foreach (var str in txtFiles.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!File.Exists(sourcePath + "\\" + str))
                {
                    DisplayMessage(string.Format("文件[{0}]不存在", str));
                    continue;
                }

                FileInfo fileInfo = new FileInfo(sourcePath + "\\" + str);

                int len = str.LastIndexOf('\\');
                if (len >= 0)
                {
                    try
                    {
                        string path = targetPath + "\\" + str.Substring(0, len);
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        fileInfo.CopyTo(path + "\\" + fileInfo.Name, true);
                        DisplayMessage(string.Format("文件[{0}]复制成功", fileInfo.Name));
                    }
                    catch (Exception ex)
                    {
                        DisplayMessage(string.Format("[{0}]Error:{1}", fileInfo.Name, ex.Message));
                    }
                }
                else
                {
                    DisplayMessage(string.Format("文件[{0}]不存在", str));
                }
            }
            DisplayMessage(string.Format("本次操作执行完毕，用时:{0}秒",sw.Elapsed.Seconds));
        }

        private void DisplayMessage(string msg)
        {
            if (txtLog.Text.Length == 0)
            {
                txtLog.Text = msg;
            }
            else
            {
                txtLog.Text += "\r\n" + msg;
            }
        }

        public static void FileCoppy(string orignFile, string newFile)
        {
            File.Copy(orignFile, newFile, true);
        }
    }
}
